// pages/goodsManagement/OfflineSetVip/OfflineSetVip.js
const Controllers = require('../../controllers/Controller.js').controller;
const formatTime3 = require('../../../utils/util.js').formatTime3
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow:false,
    isGiveBalance:false,
    isGiveIntegral:false,
    isGiveCoupon:false,
    couponList:[],
    addCouponList:[],
    vipPrice:'',
    balance:0,
    integral:0,
    chooseProduct:[],
    payList:[
      {
        name:"支付宝",
        id:0
      },
      {
        name:"微信",
        id:1
      },
      {
        name:"现金",
        id:5
      },
      {
        name:"其他",
        id:4
      },
    ],
    vipList:[
        {
          name:"否",
          id:''
        },
        {
          name:"是",
          id:0
        }
    ],
    payType:0,
    vip:"",
    index:0,
    index2:0,
    staffId:""
  },
  //隐藏弹窗
  hideAddCoupon(){
    this.setData({
      isShow:false
    })
  },
  //显示弹窗
  addCoupon(){
    if(this.data.couponList.length>0)
    this.setData({
      isShow:true
    })
    else
    wx.showToast({
      icon:"none",
      title: '暂无优惠券可选',
    })
  },
  addGoods(){
    wx.navigateTo({
      url: '/pages/settlement/AddGoods/AddGoods',
    })
  },
  //提交
  submit(){
    let data
    let that =this
    let ids = ""
    let ids2 = ""
    if(this.data.chooseProduct.length>0){
      this.setData({
        isGiveGoods:true
      })
    }else{
      this.setData({
        isGiveGoods:false
      })
    }
    this.data.addCouponList.forEach((item,index)=>{
      
      if(index==0)
      ids=ids+item.id+"-1"
      else
      ids=ids+","+item.id+"-1"
      console.log(ids)
    })
    this.data.chooseProduct.forEach((item,index)=>{
      
      if(index==0)
      ids2=ids2+(item.goodsId||item.id)+"-"+(item.count||item.num)
      else
      ids2=ids2+","+(item.goodsId||item.id)+"-"+(item.count||item.num)
      console.log(ids2)
    })
   // ids = JSON.stringify(ids)
    data = {
      price:this.data.vipPrice,
      type:2,
      haveGivePrice: this.data.isGiveBalance ? 1 : 0,
      shopId:wx.getStorageSync('shopId'),
      haveGiveIntegral: this.data.isGiveIntegral ? 1:0,
      giveIntegral:this.data.isGiveIntegral ? this.data.integral :'',
      givePrice:this.data.isGiveBalance?this.data.balance : '',
      goodsIdNum:this.data.isGiveGoods ? ids2 : '',
      haveGoods:this.data.isGiveGoods?1:0,
      couponIdNum:this.data.isGiveCoupon ? ids : '',
      haveCoupon:this.data.isGiveCoupon?1:0,
      name:"会员开卡",
      timeState:1,
      payType:this.data.payType,
      vip:this.data.vip,
      shopAccountId:this.data.staffId
    }
    if(this.data.vipPrice == ''){
      wx.showToast({
        title: '请输入开卡金额',
        icon: 'none',
        duration: 2000
      })
    }else if(this.data.isGiveIntegral && this.data.integral == ''){
      wx.showToast({
        title: '请输入赠送积分',
        icon: 'none',
        duration: 2000
      })
    }else if(this.data.isGiveBalance && this.data.balance == ''){
      wx.showToast({
        title: '请输入赠送余额',
        icon: 'none',
        duration: 2000
      })
    }else if(this.data.isGiveCoupon && ids == '[]'){
      wx.showToast({
        title: '请选择优惠券',
        icon: 'none',
        duration: 2000
      })
    }else{
        data.accountId=this.data.userId||this.data.selectItem.accountId
        wx.showModal({
          title: '提示',
          content: "是否为该用户开卡",
          success (res) {
            if (res.confirm) {
              Controllers.accountOpenVip(data).then(res=>{
                if(res.code==1){
                  wx.showToast({
                    title: '开卡成功',
                    icon: 'none',
                    duration: 2000
                  })
                  setTimeout(function(){
                    wx.navigateBack({
                      delta: 1
                    })
                  },1500)
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
    }
    
  },
  //开关
  switchGiveBalance(e){
    this.setData({
      isGiveBalance:e.detail.value
    })
  },
  switchGiveIntegral(e){
    this.setData({
      isGiveIntegral:e.detail.value
    })
  },
  switchGiveCoupon(e){
    this.setData({
      isGiveCoupon:e.detail.value
    })
  },

  switchGiveGoods(e){
    this.setData({
      isGiveGoods:e.detail.value
    })
  },
  //输入框赋值
  bindinput(e){
    let type = e.currentTarget.dataset['type'];
    let value = e.detail.value
    if(type=="vipPrice"){
      this.setData({
        vipPrice:value
      })
    }else if(type=="balance"){
      if(value){
        this.setData({
          isGiveBalance:true
        })
      }else{
        this.setData({
          isGiveBalance:false
        })
      }
      this.setData({
        balance:value
      })
    }else if(type=="integral"){
      if(value){
        this.setData({
          isGiveIntegral:true
        })
      }else{
        this.setData({
          isGiveIntegral:false
        })
      }
      this.setData({
        integral:value
      })
    }
  },
  //确认选择
  determine(){
    let addCouponList
    addCouponList = this.data.couponList.filter(item=>item.select)
    this.setData({
      addCouponList,
      isShow:false
    })
    if(this.data.addCouponList.length>0){
      this.setData({
        isGiveCoupon:true
      })
    }else{
      this.setData({
        isGiveCoupon:false
      })
    }
  },
  //选择
  select(e){
    let index = e.currentTarget.dataset['index'];
    let couponList = this.data.couponList
    couponList[index].select = !couponList[index].select
    this.setData({
      couponList
    })
  },
  //删除
  delect(e){
    let index = e.currentTarget.dataset['index'];
    let addCouponList = this.data.addCouponList
    addCouponList.splice(index,1)
    this.setData({
      addCouponList
    })
    if(this.data.addCouponList.length>0){
      this.setData({
        isGiveCoupon:true
      })
    }else{
      this.setData({
        isGiveCoupon:false
      })
    }
  },
  //获取优惠券列表
  activityPage(){
    let that =this
    Controllers.getShopCouponList(
      {shopId:wx.getStorageSync('shopId'),sessionId:wx.getStorageSync('token')}).then(res=>{
      let couponList = []
      if(res.code==1){
        res.data.forEach(item => {
          console.log(item)
          if(item.type!=0){
            // if(item.effectiveStartTime)
            // item.effectiveStartTime = formatTime3(item.effectiveStartTime)
            // if(item.effectiveEndTime)
            // item.effectiveEndTime = formatTime3(item.effectiveEndTime)
            if(item.type == 1){
              item.typeText="满减优惠券"
            }else if(item.type == 2){
              item.typeText="限时限产品券"
            }else if(item.type == 3){
              item.typeText="体验券"
            }else if(item.type == 4){
              item.typeText="活动礼包券"
            }else if(item.type == 5){
              item.typeText="拉新券"
            }
            couponList.push(item)
          }
        });
        this.setData({
          couponList
        })
      }
    })
  },
    getArrEqual(arr1, arr2) {
        let newArr = [];
        for (let i = 0; i < arr2.length; i++) {
            for (let j = 0; j < arr1.length; j++) {
                if(arr1[j].id === arr2[i].id){
                  arr2[i].select=true
                }
        }
    }
 return arr2;
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.accountId){
      this.setData({
        userId:options.accountId,
        'selectItem.mobile':options.accountMobile,
        'selectItem.nick':options.accountNick,
        'selectItem.balance':options.balance,
        'selectItem.vip':options.vip
      })
    }
    this.activityPage()
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getStaffList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }

  showPopup() {
    this.setData({
      show: true
    });
  },
  bindKeyInput(res) {
    console.log(res)
    this.setData({
      input: res.detail.value
    })
  },
  optionTap(res) {
    var that = this;
    Controllers.selectByMobile({
      sessionId: wx.getStorageSync('token'),
      shopId: wx.getStorageSync('shopId'),
      mobile: this.data.input
    }).then(res => {
      that.setData({
        userList: res.data
      })

    })
  },

  selectUser(res){
    this.setData({
      selectId: res.currentTarget.dataset.item.id,
      selectItem: res.currentTarget.dataset.item,
      userId:res.currentTarget.dataset.item.accountId,
      show:false,
      showAdd:true
    })
   },

   bindPaychange(e){
     let index=e.detail.value
     let list=this.data.payList
     this.setData({
       index:index,
       payType:list[index].id
     })
    console.log(e)
   },

   bindVipchange(e){
    let index=e.detail.value
    let list=this.data.vipList
    this.setData({
      index2:index,
      vip:list[index].id
    })
   console.log(e)
  },

  getStaffList(){
    let shopId=wx.getStorageSync('shopId')
    Controllers.getStaffList({shopId:shopId}).then(res=>{
      this.setData({
        list:res.data
      })
    })
  },
  //关闭员工选框
  closeOpen(){
    this.setData({
      staffBox:false
    })
  },

  //显示弹框
  showModal: function() {
    this.setData({
      showModalStatus: true
    })
  },
  // 隐藏弹框
  hideModal: function() {
      this.setData({
        showModalStatus: false
      })
  },

  //选择员工
  selectStaff(e){
    let index=e.currentTarget.dataset.index
    console.log(e)
    if(index===this.data.selectIndex){
      this.setData({
        selectIndex:"",
        staffId:"",
        staffName:""
      })
    }else{
      this.setData({
        selectIndex:index
      })
    }
  },

  //确定员工
  defineStaff(){
    if(this.data.selectIndex||this.data.selectIndex===0){
      this.setData({
        staffId:this.data.list[this.data.selectIndex].id,
        staffName:this.data.list[this.data.selectIndex].nick
      })
    }
    this.hideModal()
  },

  //跳转员工详情
  selectStaffDetail(e){
    let id=e.currentTarget.dataset.id
    let index=e.currentTarget.dataset.index
    let value=this.data.list[index]
    wx.navigateTo({
      url: '/pages/selecStaffDetail/selecStaffDetail?staffDetail='+encodeURIComponent(JSON.stringify(value)),
    })
  }
})